package cn.flashk.controls.managers {
	import cn.flashk.controls.support.ColorConversion;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;

	public class SkinLoader {
		public static const SKIN_LOADED:String = "skinLoaded";
		public static const eventDispatcher:EventDispatcher = new EventDispatcher();
		public static var isSelfFile:Boolean = false;
		private static var ldr:Loader;

		public function SkinLoader(){
		}

		/**
		 * 加载皮肤文件 
		 * @param path
		 * @param bytes
		 * @param context
		 * 
		 */		
		public static function loadSkinFile(path:String, bytes:ByteArray = null, context:LoaderContext = null):void {
			ldr = new Loader();
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, initSkinSet);
			if (bytes == null){
				ldr.load(new URLRequest(path), context);
			} else {
				ldr.loadBytes(bytes, context);
			}
		}

		/**
		 * 获取链接的sprite对象  
		 * @param name
		 * @return 
		 * 
		 */		
		public static function getSpriteFromSkinFile(name:String):Sprite {
			var classRef:Class;
			try {
				if (isSelfFile == false){
					classRef = ldr.contentLoaderInfo.applicationDomain.getDefinition(name) as Class;
				} else {
					classRef = getDefinitionByName(name) as Class
				}
				return new classRef() as Sprite;
			} catch (e:Error){
			}
			return null;
		}

		/**
		 * 获取链接的类值 
		 * @param name
		 * @return 
		 * 
		 */		
		public static function getClassFromSkinFile(name:String):Class {
			try {
				if (isSelfFile == false){
					return ldr.contentLoaderInfo.applicationDomain.getDefinition(name) as Class;
				}
				return getDefinitionByName(name) as Class;
			} catch (e:Error){
			}
			return null;
		}

		/**
		 * 获取链接的位图数据 
		 * @param name
		 * @return 
		 * 
		 */		
		public static function getBitmapData(name:String):BitmapData {
			var classRef:Class;
			try {
				if (isSelfFile == false){
					classRef = ldr.contentLoaderInfo.applicationDomain.getDefinition(name) as Class;
				} else {
					classRef = getDefinitionByName(name) as Class;
				}
				var dis:DisplayObject = new classRef() as DisplayObject;
				var bd:BitmapData = new BitmapData(dis.width, dis.height, true, 0);
				bd.draw(dis);
				return bd;
			} catch (e:Error){
			}
			return null;
		}

		/**
		 * 设置按钮字体状态颜色 
		 * @param xml
		 * 
		 */		
		public static function setSkinStyle(xml:XML):void {
			DefaultStyle.buttonOutTextColor = xml.buttonTextColor;
			DefaultStyle.buttonOverTextColor = xml.buttonOverTextColor;
			DefaultStyle.buttonDownTextColor = xml.buttonPressTextColor;
			DefaultStyle.menuBackgroundColor = xml.menuBackgroundColor;
			SkinThemeColor.itemOverTextColor = ColorConversion.transformWebColor(xml.itemOverTextColor);
			SkinThemeColor.border = ColorConversion.transformWebColor(xml.borderColor);
		}

		protected static function initSkinSet(event:Event):void {
			initSWFSkinSet(ldr);
		}

		/**
		 * 设置加载皮肤状态
		 * 设置按钮文本状态颜色 
		 * 派发加载完成事件
		 * @param loader
		 * 
		 */		
		public static function initSWFSkinSet(loader:Loader):void {
			SkinManager.isUseDefaultSkin = false;
			ldr = loader;
			var obj:Object = loader.content;
			
			var xml:XML = obj.skinSet as XML;		//皮肤中的xml
			setSkinStyle(xml);
			eventDispatcher.dispatchEvent(new Event(SKIN_LOADED));
			isSelfFile = false;
		}
	}
}